import '../utils/device_data_util.dart';

import 'extension_string.dart';

/// @author zdl
/// date 2020/11/10 16:00
/// email zdl328465042@163.com
/// description 扩展num
extension ExtensionNum on num {
  static String zhCn = '零一二三四五六七八九';
  static String zhFt = '零壹贰叁肆伍陆柒捌玖';

  ///阿拉伯数字转中文
  String toZh({bool isCn = true}) {
    String zh = '';
    String numStr = toString();
    for (int i = 0; i < numStr.length; i++) {
      var index = numStr.findChar(i + 1).toInt;
      zh += index == null ? '点' : (isCn ? zhCn[index] : zhFt[index]);
    }
    return zh;
  }

  /// 月份转中文
  String toTimeZh() {
    String zh = '';
    String numStr = toString();
    if (numStr.length == 1) {
      zh = zhCn[numStr.toInt!];
    } else if (numStr == '10') {
      zh = '十';
    } else if (numStr.length == 2) {
      zh = '十${zhCn[numStr.findChar(2).toInt!]}';
    }
    return zh;
  }

  ///适配不同分辨率的手机像素
  double toFit({int uiWidth = 750}) {
    var ratio = ZDeviceDataUtil.screenWidth / uiWidth;
    return this * ratio;
  }

  bool get isMonth => this is int && this >= 1 && this <= 12;

  bool get isYear => this is int && this >= 0 && this <= 10000;

  String get toHex => '${this < 16 ? '0' : ''}${toInt().toRadixString(16)}';

  ///适配不同分辨率的手机像素
  double get toPx => this * (ZDeviceDataUtil.screenWidth / 750);

  ///秒转分秒
  String toMs({String mUnit = '分钟', String sUnit = '秒'}) {
    int m = 0;
    int s = 0;
    if (this >= 60) {
      m = this ~/ 60;
      s = (this % 60).toInt();
    } else {
      s = this.toInt();
    }
    String value = '$s$sUnit';
    if (0 != m) value = '$m$mUnit$value';
    return value;
  }

  ///秒转时分秒
  String toHms({String hUnit = '小时', String mUnit = '分钟', String sUnit = '秒'}) {
    int h = 0;
    int m = 0;
    int s = 0;
    if (this >= 60 * 60) {
      h = this ~/ (60 * 60);
      m = (this ~/ 60) % 60;
      s = (this % 60).toInt();
    } else if (this >= 60) {
      m = this ~/ 60;
      s = (this % 60).toInt();
    } else {
      s = this.toInt();
    }
    String value = '$s$sUnit';
    if (0 != m) value = '$m$mUnit';
    if (0 != h) value = '$h$hUnit';
    return value;
  }
}
